<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="盐值" prop="salt">
      <el-input v-model="dataForm.salt" placeholder="盐值"></el-input>
    </el-form-item>
    <el-form-item label="账户名" prop="accountName">
      <el-input v-model="dataForm.accountName" placeholder="账户名"></el-input>
    </el-form-item>
    <el-form-item label="真实姓名" prop="accountRealname">
      <el-input v-model="dataForm.accountRealname" placeholder="真实姓名"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="accountSex">
      <el-input v-model="dataForm.accountSex" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="accountPhone">
      <el-input v-model="dataForm.accountPhone" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="地址，由身份信息获取" prop="accountAddress">
      <el-input v-model="dataForm.accountAddress" placeholder="地址，由身份信息获取"></el-input>
    </el-form-item>
    <el-form-item label="生日格式如20220224，由身份信息获取" prop="accountBirthday">
      <el-input v-model="dataForm.accountBirthday" placeholder="生日格式如20220224，由身份信息获取"></el-input>
    </el-form-item>
    <el-form-item label="开户时间" prop="accountOpentime">
      <el-input v-model="dataForm.accountOpentime" placeholder="开户时间"></el-input>
    </el-form-item>
    <el-form-item label="余额" prop="accountBalance">
      <el-input v-model="dataForm.accountBalance" placeholder="余额"></el-input>
    </el-form-item>
    <el-form-item label="账户密码" prop="accountPassword">
      <el-input v-model="dataForm.accountPassword" placeholder="账户密码"></el-input>
    </el-form-item>
    <el-form-item label="0销户，1冻结，2未登录，3已登录" prop="accountStatus">
      <el-input v-model="dataForm.accountStatus" placeholder="0销户，1冻结，2未登录，3已登录"></el-input>
    </el-form-item>
    <el-form-item label="电子邮箱" prop="accountEmail">
      <el-input v-model="dataForm.accountEmail" placeholder="电子邮箱"></el-input>
    </el-form-item>
    <el-form-item label="个人简介" prop="accountInfo">
      <el-input v-model="dataForm.accountInfo" placeholder="个人简介"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          accountId: 0,
          salt: '',
          accountName: '',
          accountRealname: '',
          accountSex: '',
          accountPhone: '',
          accountAddress: '',
          accountBirthday: '',
          accountOpentime: '',
          accountBalance: '',
          accountPassword: '',
          accountStatus: '',
          accountEmail: '',
          accountInfo: ''
        },
        dataRule: {
          salt: [
            { required: true, message: '盐值不能为空', trigger: 'blur' }
          ],
          accountName: [
            { required: true, message: '账户名不能为空', trigger: 'blur' }
          ],
          accountRealname: [
            { required: true, message: '真实姓名不能为空', trigger: 'blur' }
          ],
          accountSex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          accountPhone: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          accountAddress: [
            { required: true, message: '地址，由身份信息获取不能为空', trigger: 'blur' }
          ],
          accountBirthday: [
            { required: true, message: '生日格式如20220224，由身份信息获取不能为空', trigger: 'blur' }
          ],
          accountOpentime: [
            { required: true, message: '开户时间不能为空', trigger: 'blur' }
          ],
          accountBalance: [
            { required: true, message: '余额不能为空', trigger: 'blur' }
          ],
          accountPassword: [
            { required: true, message: '账户密码不能为空', trigger: 'blur' }
          ],
          accountStatus: [
            { required: true, message: '0销户，1冻结，2未登录，3已登录不能为空', trigger: 'blur' }
          ],
          accountEmail: [
            { required: true, message: '电子邮箱不能为空', trigger: 'blur' }
          ],
          accountInfo: [
            { required: true, message: '个人简介不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.accountId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.accountId) {
            this.$http({
              url: this.$http.adornUrl(`/expect/account/info/${this.dataForm.accountId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.salt = data.account.salt
                this.dataForm.accountName = data.account.accountName
                this.dataForm.accountRealname = data.account.accountRealname
                this.dataForm.accountSex = data.account.accountSex
                this.dataForm.accountPhone = data.account.accountPhone
                this.dataForm.accountAddress = data.account.accountAddress
                this.dataForm.accountBirthday = data.account.accountBirthday
                this.dataForm.accountOpentime = data.account.accountOpentime
                this.dataForm.accountBalance = data.account.accountBalance
                this.dataForm.accountPassword = data.account.accountPassword
                this.dataForm.accountStatus = data.account.accountStatus
                this.dataForm.accountEmail = data.account.accountEmail
                this.dataForm.accountInfo = data.account.accountInfo
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/expect/account/${!this.dataForm.accountId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'accountId': this.dataForm.accountId || undefined,
                'salt': this.dataForm.salt,
                'accountName': this.dataForm.accountName,
                'accountRealname': this.dataForm.accountRealname,
                'accountSex': this.dataForm.accountSex,
                'accountPhone': this.dataForm.accountPhone,
                'accountAddress': this.dataForm.accountAddress,
                'accountBirthday': this.dataForm.accountBirthday,
                'accountOpentime': this.dataForm.accountOpentime,
                'accountBalance': this.dataForm.accountBalance,
                'accountPassword': this.dataForm.accountPassword,
                'accountStatus': this.dataForm.accountStatus,
                'accountEmail': this.dataForm.accountEmail,
                'accountInfo': this.dataForm.accountInfo
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
